package com.bs.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.codehaus.jettison.json.JSONObject;
import java.sql.Timestamp;
import java.util.*;
import javax.servlet.http.*;
import javax.servlet.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.*;
import com.bs.bean.*;
import com.bs.service.CommentService;
import com.bs.utils.*;
/*控制层：CommentController*/
@RestController
@RequestMapping(value="/json/comment",produces="application/json; charset=utf-8")
public class CommentJsonController {
//	注入CommentService对象
	@Autowired
	private CommentService commentService;
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private HttpSession session;
	@Autowired
	private ServletContext context;
//	查询数据返回到/page/comment/commentItem.jsp页面
	@RequestMapping("/findAll")
	public Result findAll(Comment comment, @RequestParam(required = false, defaultValue = "true") Boolean mode) {
		try {
			List<Comment> list = commentService.findAll(comment,mode);
			return Result.success().put("list",list);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result.fail(e.getMessage());
		}
		
	}
	@RequestMapping("/findById")
	public Result findById(Integer id) {
		try {
			Comment comment = commentService.findById(id);
			return Result.success().put("comment",comment);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return Result.fail(e.getMessage());
		}
	}
//	删除数据
	public void deleteById(String idString) {
		if (idString!=null) {
			Integer id = Integer.valueOf(idString);
			//	删除数据库中comment中的对应id的数据
			commentService.deleteById(id);
		}
	}
//	根据commentItem.jsp页面传递的id，调用commentServlet#deleteById(id)方法删除comment表中的数据，重定向到findAll请求
	@RequestMapping("/deleteById")
	public Result deleteById() {
//		单条删除
		deleteById(request.getParameter("id"));
//		批量删除
		String ids = request.getParameter("ids");
		if (ids!=null) {
			for (String id : ids.split(",")) {
				deleteById(id);
			}
		}
			return Result.success();
	}
	@RequestMapping("/delete")
	public Result delete(Comment comment) {
		try {
			commentService.delete(comment);
			return Result.success("删除成功");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return Result.fail(e.getMessage());
		}
	}
	@RequestMapping("/deleteByIds")
	public Result deleteByIds(List<Integer> list) {
		try {
			commentService.deleteByIds(list);
			return Result.success("删除成功");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return Result.fail(e.getMessage());
		}
	}
	@RequestMapping("/updateByIdSelective")
	public Result updateByIdSelective(Comment comment) {
		try {
			commentService.updateByIdSelective(comment);
			return Result.success("更新成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail(e.getMessage());
		}
	}
	
//	根据commentUpdate.jsp页面传递的数据，更新数据库，重定向到findAll请求
	@RequestMapping("/updateById")
	public Result updateById(Comment comment) {
		try {
			commentService.updateByIdSelective(comment);
		} catch (Exception e) {
			e.printStackTrace();
		}
			return Result.success();
	}

//	根据commentInsert.jsp页面传递的数据，封装在comment对象中插入数据库，重定向到重定向到findAll请求
	@RequestMapping("/insert")
	public Result insert(Comment comment) {
		try {
			commentService.insert(comment);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.fail(e.getMessage());
		}
			return Result.success();
	}
}